package cn.exrick.xboot.common.utils;
import java.util.HashMap;
import java.util.concurrent.*;

public class ThreadPoolSingleton02 {

    // 私有构造方法（禁止外部实例化）
    private ThreadPoolSingleton02() {
        throw new UnsupportedOperationException("禁止反射破坏单例！");
    }

    // 静态内部类持有线程池实例
    private static class Holder {
        // 配置参数（可提取到外部常量）
        private static final int CORE_POOL_SIZE = 5;
        private static final int MAX_POOL_SIZE = 10;
        private static final int QUEUE_CAPACITY = 100;
        private static final long KEEP_ALIVE_TIME = 2000L;

        // 初始化线程池实例
        static final ThreadPoolExecutor INSTANCE = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        static {
            INSTANCE.prestartAllCoreThreads(); // 预启动核心线程
        }
    }

    // 全局访问点
    public static ThreadPoolExecutor getInstance() {
        return Holder.INSTANCE;
    }
}